import requestProxy from '@/utils/request';
import { setChildren } from '@/utils/tools';

const { request } = requestProxy;

// 资源列表
export function getList(page, perPage, params) {
  return request({
    url: '/admin/resource/list',
    method: 'get',
    params: {
      page,
      perPage,
      attributionMatrix: params.attributionMatrix,
      resourceId: params.resourceId,
      resourceName: params.resourceName,
      platformTypeName: params.platformTypeName,
      platformType: params.platformType,
      accountBelong: params.accountBelong,
      createStart: params.createStart,
      createEnd: params.createEnd,
      accountBelongName: params.accountBelongName,
      executorId: params.executorId
    }
  });
}

// 新增资源信息
export function addResource(data) {
  return request({
    url: '/admin/resource/create',
    method: 'post',
    data
  });
}

// 编辑资源信息
export function editResource(id, data) {
  return request({
    url: `/admin/resource/${id}/edit`,
    method: 'put',
    data
  });
}

// 获取资源信息
export function getResouceInfo(id, params) {
  return request({
    url: `/admin/resource/info/${id}`,
    method: 'get',
    params
  });
}

// 导出资源
export function exportList({ attributionMatrix, resourceId, resourceName, createStart, createEnd }) {
  return request({
    url: 'admin/resource/export',
    method: 'get',
    params: {
      attributionMatrix,
      resourceId,
      resourceName,
      createStart,
      createEnd
    }
  });
}

// 获取所属部门
export function getDeptInfo(id) {
  return request({
    url: 'admin/attributionDept/list',
    method: 'get'
  });
}

// 获取运营模式
export function getOperateoptions() {
  return new Promise((resolve, reject) => {
    request({
      url: 'admin/resource/operate/options',
      method: 'get'
    }).then(res => {
      // 如果 children 字段为空数组时，需要设置成 null
      if (res.data && res.data.length) {
        res.data.forEach(i => {
          setChildren(i);
        });
      }
      resolve(res);
    }).catch(err => {
      reject(err);
    });
  });
}

// 新资源列表
export function getResourceList(data, cancelToken) {
  return request({
    url: '/admin/resource/manage',
    method: 'post',
    data,
    cancelToken
  });
}
// 新资源列表
export function getResourceListPost(data, cancelToken) {
  return request({
    url: '/admin/resource/manage',
    method: 'post',
    data,
    cancelToken
  });
}
// 批量设置资源状态
export function setResourceStatus(data) {
  return request({
    url: '/admin/resource/batchStatus',
    method: 'put',
    data
  });
}

// 合并资源列表
export function getMergeList(params) {
  return request({
    url: '/admin/resource/merge/list',
    method: 'get',
    params
  });
}
export function merge(data) {
  return request({
    url: '/admin/resource/merge',
    method: 'post',
    data
  });
}

// 资源信息-变更记录-账号改名列表
export function getAccountNameChangeList(params = '') {
  return request({
    url: 'admin/resource/accountName/changeList',
    method: 'get',
    params
  });
}

// 资源信息-变更记录-账号改名列表-添加
export function createAccountNameChangeList(data) {
  return request({
    url: 'admin/resource/accountName/changeList/create',
    method: 'post',
    data
  });
}

// 资源信息-变更记录-账号改名列表-删除
export function deleteAccountNameChangeList(id) {
  return request({
    url: `admin/resource/accountName/changeList/delete/${id}`,
    method: 'post'
  });
}

// 资源信息-变更记录-账号改名列表-编辑
export function editAccountNameChangeList(data) {
  return request({
    url: 'admin/resource/accountName/changeList/edit',
    method: 'post',
    data
  });
}

// 资源列表-案例(订单)列表
export function getOrderList(params = '') {
  return request({
    url: 'admin/resource/order/list',
    method: 'get',
    params
  });
}
// 账户资源新建编辑检测
export function hasResource(params) {
  return request({
    url: 'admin/resource/hasresource',
    method: 'get',
    params
  });
}

// 获取资源是否有编辑权限
export function getResourceEditAuth(data) {
  return request({
    url: 'admin/resource/batch/authority',
    method: 'post',
    data
  });
}

// 合并资源【新-v2.9.3.1】
export function newMerge(data) {
  return request({
    url: 'admin/resource/newmerge',
    method: 'post',
    data
  });
}
// 是否开启资源采集
export function autoPick(data) {
  return request({
    url: 'admin/resource/autoPick',
    method: 'put',
    data
  });
}
// 设置签约
export function setSignStatus(data) {
  return request({
    url: 'admin/resource/signStatus/set',
    method: 'post',
    data
  });
}

// 快速发起询单/询价过滤未入驻账号
export function resourceFilter(params) {
  return request({
    url: 'admin/resource/resourceFilter',
    method: 'get',
    params
  });
}

// 获取资源列表动态字段
export function getDynamickey(params) {
  return request({
    url: 'admin/platformField/dynamicKey',
    method: 'get',
    params
  });
}

export function getViewList(params) {
  return request({
    url: '/admin/resource/view/list',
    method: 'get',
    params
  });
}
export function createViewList(data) {
  return request({
    url: 'admin/resource/view/create',
    method: 'post',
    data
  });
}

export function editViewList(data) {
  return request({
    url: 'admin/resource/view/edit',
    method: 'put',
    data
  });
}
export function deleteView(id) {
  return request({
    url: `admin/resource/view/delete/${id}`,
    method: 'delete'
  });
}
// 编辑账号信息（资源编辑部分字段）
export function editAccountInfo(data) {
  return request({
    url: `/admin/resource/accountInfo/edit`,
    method: 'put',
    data
  });
}
// 编辑更多账号信息（资源编辑部分字段）
export function editmoreAccountInfo(data) {
  return request({
    url: `/admin/resource/moreAccountInfo/edit`,
    method: 'put',
    data
  });
}
// 编辑上刊信息（资源编辑部分字段）
export function editJournalInfo(data) {
  return request({
    url: `/admin/resource/journalInfo/edit`,
    method: 'put',
    data
  });
}
// 编辑签约信息（资源编辑部分字段）
export function editSignInfo(data) {
  return request({
    url: `/admin/resource/signInfo/edit`,
    method: 'put',
    data
  });
}
// 编辑更多信息（资源编辑部分字段）
export function editResourceDataInfo(data) {
  return request({
    url: 'admin/resource/otherManageInfo/edit',
    method: 'put',
    data
  });
}
export function getStatusLog(id, params) {
  return request({
    url: `admin/resource/statusLog/${id}`,
    method: 'get',
    params
  });
}
export function getJournalStatusLog(id, params) {
  return request({
    url: `admin/resource/journalStatusLog/${id}`,
    method: 'get',
    params
  });
}
// 获取统计字段
export function getStatisticsData(data) {
  return request({
    url: 'admin/resource/list/statistics',
    method: 'post',
    data
  });
}
